<template>
  <div>
    <div>
      <!-- 查询、清空、团队类型登记按钮 -->
      <el-input v-model="params.team_type" placeholder="请输入团队类型" style="width: 200px; margin-left: 50px"></el-input>
      <el-button type="warning" style="margin-left: 5px" @click="findBySearch()">查询</el-button>
      <el-button type="primary" style="margin-left: 5px" @click="reset()">清空</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="team_details()">查看团队详情</el-button>
      <el-button type="warning" style="margin-left: 700px" @click="add()">团队类型登记</el-button>
      <!-- 编辑和删除按钮移到顶部 -->
      <el-button type="primary" :disabled="!hasSelectedRows" @click="updateSelected()">编辑</el-button>
      <el-button type="danger" :disabled="!hasSelectedRows" @click="delSelected()">删除</el-button>
    </div>
    <div>
      <!-- 表格部分，移除了底部的编辑和删除按钮 -->
      <el-table
          :data="tableData"
          style="width: 100%"
          ref="teamTable"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column
            prop="team_typeid"
            label="团队类型ID"
            width="200"
        ></el-table-column>
        <el-table-column
            prop="team_type"
            label="团队类型"
            width="150"
        ></el-table-column>
        <!-- 移除了操作列和其中的编辑、删除按钮 -->
      </el-table>
    </div>
    <!-- 分页部分 -->
    <div style="margin-top: 10px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import teamdetails from "@/views/DataFactory/compoments/Teammangement/Teamdetails.vue";

export default {
  data() {
    return {
      total: 0,
      form: {},
      params: {
        team_port: '',
        team_type: '',
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      multipleSelection: [], // 添加一个数组来跟踪选中的行
    };
  },
  created() {
    this.findBySearch();
    this.hasSelectedRows();
  },
  methods: {
    team_details(){
      this.$router.push('team_details')
    },
    add(){
      this.$router.push('type_add')
    },
    back() {
      this.$router.go(-1);
    },
    del(team_typeid){
      request.delete("http://localhost:9100/data_factory/team_details/type_delete/" + team_typeid).then(res=>{
        if (res.code ==='0'){
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.findBySearch()
        }else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })

    },

    update(team_typeid){
      // 发送数据的页面
      this.$router.push({ name: 'team_details/type_update', query: { team_typeid: team_typeid } });
      // this.$router.push({name :'team_details/update', params:{team_id:team_id}})
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    reset() {
      this.params = {
        team_port: '',
        team_type: '',
        pageNum: 1,
        pageSize: 5
      }

      this.findBySearch();
    },
    findBySearch() {
      request.get("http://localhost:9100/data_factory/team_details/team_type", {
        params: this.params

      }).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list;
          this.total = res.data.total;
          //接收的数据.渲染在了上面element组件的表格里
        } else {

        }
      })
    },
    hasSelectedRows(){
      // 计算属性来判断是否有选中的行
      return this.multipleSelection.length > 0;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val; // 更新选中的行数组
    },
    updateSelected() {
      if (this.multipleSelection.length > 0) {
        // 假设我们只允许一次编辑一个项目，所以取第一个选中的项目
        const team_typeid = this.multipleSelection[0].team_typeid;
        this.update(team_typeid); // 调用原有的update方法
      }
    },
    delSelected() {
      if (this.multipleSelection.length > 0) {
        // 可以批量删除或逐个删除，这里以逐个删除为例
        this.multipleSelection.forEach(row => {
          this.del(row.team_typeid); // 调用原有的del方法
        });
        // 清空选中状态，因为删除后原来的选中项已经不存在了
        this.multipleSelection = [];
      }
    },
  }
}
</script>

<style scoped>

</style>